package com.acwing.lc.part1;

import com.acwing.lc.common.ListNode;

/**
 * @author rkc
 * @date 2022/9/10 10:19
 */
public class LC19删除链表的倒数第N个结点 {

    public ListNode removeNthFromEnd(ListNode head, int k) {
        ListNode dummy = new ListNode(-1, head);
        ListNode l = dummy, r = dummy;
        for (int i = 0; i < k + 1; i++) r = r.next;
        while (r != null) {
            r = r.next;
            l = l.next;
        }
        l.next = l.next.next;
        return dummy.next;
    }
}
